/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-4 下午5:04
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.activities;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.example.dqhl.jdmetro.JDMetroApplication;
import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.activities.life.LifeDetailActivity;
import com.example.dqhl.jdmetro.adapter.MyCollectionAdapter;
import com.example.dqhl.jdmetro.base.BaseActivity;
import com.example.dqhl.jdmetro.bean.MyCollection;
import com.example.dqhl.jdmetro.config.Config;
import com.example.dqhl.jdmetro.utils.Dlog;
import com.example.dqhl.jdmetro.utils.JsonUtils;
import com.example.dqhl.jdmetro.utils.JumpItent;
import com.example.dqhl.jdmetro.utils.Toastutils;
import com.example.dqhl.jdmetro.utils.recycleview.ConsulationRecycleViewLayoutManager;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * description:w我的收藏
 * autour:guanluocang
 * date:17/7/14 18:02
 */
public class MyCollectionActivity extends BaseActivity {

    private final static int REQUEST_CODE_PERMISSION_LOCATION = 300;
    private Context context;
    @Bind(R.id.recycleView_myCollect)
    RecyclerView recycleView_myCollect;

    private List<MyCollection.DataBean> myCollectionList = new ArrayList<>();
    private MyCollectionAdapter myCollectionAdapter;

    //地图locationclient
    private AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    private AMapLocationClientOption mLocationOption = null;
    private Double latitude;
    private Double longitude;

    @Override
    public void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_collection);
        ButterKnife.bind(this);
        context = this;
        initTitleBar(R.string.txt_myCollection);
        setRightTextVisibility(View.GONE);
        checkLocationPermission();
    }

    @Override
    public void initData() {
        myCollectionAdapter = new MyCollectionAdapter(context);
        recycleView_myCollect.setAdapter(myCollectionAdapter);
        ConsulationRecycleViewLayoutManager layoutManager = new ConsulationRecycleViewLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView_myCollect.setLayoutManager(layoutManager);
        recycleView_myCollect.setItemAnimator(new DefaultItemAnimator());
        recycleView_myCollect.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recycleView_myCollect.setNestedScrollingEnabled(false);
//        recycleView_myCollect.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
//        recycleView_myCollect.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
//        recycleView_myCollect.setArrowImageView(R.drawable.iconfont_downgrey);
        doData();
    }

    private void doData() {
        RequestParams params = new RequestParams(Config.my_collect);
        params.addBodyParameter("id_auth", user.getId_auth());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                if (errCode == 200) {
                    MyCollection myCollection = new Gson().fromJson(result, MyCollection.class);
                    myCollectionList.addAll(myCollection.getData());
                    myCollectionAdapter.refresh_info(myCollectionList);
                    myCollectionAdapter.setOnItemClickListener(new MyCollectionAdapter.onItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            //    类型 a直播 b资讯 c生活
                            if ("a".equals(myCollectionList.get(position).getTheme_type())) {
                                Bundle bundle = new Bundle();
                                bundle.putString("id", myCollectionList.get(position).getId());
                                JumpItent.jump(MyCollectionActivity.this, LiveDetailActivity.class, bundle);
                            } else if ("b".equals(myCollectionList.get(position).getTheme_type())) {
                                Bundle bundle = new Bundle();
                                bundle.putString("info_id", myCollectionList.get(position).getId());
                                JumpItent.jump(MyCollectionActivity.this, ConsulationDeatilActivity.class, bundle);
                            } else {
                                if (latitude == null || "".equals(latitude)) {
                                    Toastutils.show("正在定位...");
                                } else {
                                    Bundle bundle = new Bundle();
                                    bundle.putString("shop_id", myCollectionList.get(position).getId());
                                    bundle.putDouble("latitude", latitude);
                                    bundle.putDouble("longitude", longitude);
                                    JumpItent.jump(MyCollectionActivity.this, LifeDetailActivity.class, bundle);
                                }

                            }
                        }
                    });
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void initListener() {

    }

    /**
     * 检查定位权限
     */
    private void checkLocationPermission() {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MyCollectionActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    REQUEST_CODE_PERMISSION_LOCATION);
        } else {
            initLocation();
        }
    }

    /**
     * 定位
     */
    private void initLocation() {
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //获取联网超时时间
        mLocationOption.setHttpTimeOut(2000);
        //获取最近3s内精度最高的一次定位结果
        mLocationOption.setOnceLocationLatest(true);
        mLocationClient = JDMetroApplication.getInstance().mLocationClient;
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //设置定位回调监听
        mLocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation != null) {
                    if (aMapLocation.getErrorCode() == 0) {
//                        address = aMapLocation.getAddress();
                        latitude = aMapLocation.getLatitude();
                        longitude = aMapLocation.getLongitude();
//                        Dlog.e("当前位置:" + address);
//                        tv_address.setText(address);
                    } else {
//                        address = "定位失败";
//                        tv_address.setText(address);
                        Dlog.e("定位失败");
                        Dlog.e("location Error, ErrCode:" + aMapLocation.getErrorCode()
                                + ", errInfo:" + aMapLocation.getErrorInfo());
                    }
                }
            }
        });
        //启动定位
        mLocationClient.startLocation();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
